<?php
namespace app\helpers;
use app\models\Push;
use JPush\Client;
class JPush{

    const JPUSH_APP_KEY = 'f430f539b60b3ae940ff86ab';
    const JPUSH_MASTER_SECRET = '028c2cd0bf379e75c586ba56';

    public static $prefix = 'jp';
    public static function pushSingleAccountIos($title,$content,$alias)
    {
        $client = new Client(self::JPUSH_APP_KEY, self::JPUSH_MASTER_SECRET);
        try {
            $response = $client->push()
                ->setPlatform(['ios', 'android'])
                ->addAlias($alias)
                ->iosNotification(["title" => $content,"body"=>Date('Y-m-d G:i:s'),"content-available"=>1], [
                    'sound' => 'default',
                    'category' => $content,
                    'content-available' => true,
                    'mutable-content' => true,
                    'extras' => [
                        'key' => 'value',
                        'jiguang'
                    ],
                ])
                ->options(array(
                    'apns_production' => true,
                ))
                ->send();
            return ['ret_code'=>0];
        } catch (\JPush\Exceptions\APIConnectionException $e) {
            return ['ret_code'=>48];
        } catch (\JPush\Exceptions\APIRequestException $e) {
            return ['ret_code'=>48];
        }
    }
    //下发多个账号， IOS下发多个账号参考DemoPushSingleAccountIOS进行相应修改
    public static function pushAccountListIos($title,$content,array $accountList)
    {
        return self::pushSingleAccountIos($title,$content,$accountList);
    }

    //下发单个账号（安卓）
    public static function pushSingleAccountAndroid($title,$content,$account)
    {
        $client = new Client(self::JPUSH_APP_KEY, self::JPUSH_MASTER_SECRET);
        try {
            $response = $client->push()
                ->setPlatform(array('android'))
                ->addAlias($account)
                ->androidNotification(
                    $content,
                    array(
                        'title'=>$title
                    )
                )
                ->send();
            return ['ret_code'=>0];
        } catch (\JPush\Exceptions\APIConnectionException $e) {
            return ['ret_code'=>48];
        } catch (\JPush\Exceptions\APIRequestException $e) {
            return ['ret_code'=>48];
        }
    }

    //下发多个账号(安卓)
    function pushAccountListAndroid($title,$content,array $accountList)
    {
        return self::pushSingleAccountIos($title,$content,$accountList);
    }


    public static function pushSingleAccount($title,$content,$account,$activity = Push::TYPE_ADMIN)
    {
        $client = new Client(self::JPUSH_APP_KEY, self::JPUSH_MASTER_SECRET);
        try {
            $response = $client->push()
                ->setPlatform(array('android','ios'))
                ->addAlias($account)
                ->androidNotification(
                    $content,
                    array(
                        'title'=>$title,
                        'extras' => array(
                            'activity' => strval($activity),
                            'AAAA'=>'00000'
                        ),
                    )
                )
                ->iosNotification(["title" => $content,"body"=>Date('Y-m-d G:i:s'),"content-available"=>1], [
                    'sound' => 'default',
                    'category' => $content,
                    'content-available' => true,
                    'mutable-content' => true,
                    'extras' => [
                        'key' => 'value',
                        'jiguang'
                    ],
                ])
                ->send();
            return ['ret_code'=>0];
        } catch (\JPush\Exceptions\APIConnectionException $e) {
            return ['ret_code'=>48];
        } catch (\JPush\Exceptions\APIRequestException $e){
            return ['ret_code'=>48,'message'=>$e->getMessage()];
        }
    }

    //下发多个账号(安卓)
    public static function pushAccountList($title,$content,array $accountList,$activity = Push::TYPE_ADMIN)
    {
        return self::pushSingleAccount($title,$content,$accountList,$activity);
    }
}